//
//  COSignInViewController.swift
//  CocoProject
//
//  Created by Ruthless on 2023/8/18.
//

import UIKit


class COSignInViewController: COBaseViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()

    
    }
    
    override func configUI() {
        view.backgroundColor = .rgba(17, 7, 41)
        
        view.addSubview(nameLabel)
        view.addSubview(emailLabel)
        view.addSubview(emailTextField)
        view.addSubview(pswLabel)
        view.addSubview(pswTextField)
        view.addSubview(signInButton)
        view.addSubview(signUpButton)
        view.addSubview(delegateLabel)
        
        nameLabel.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(21)
            make.top.equalToSuperview().offset(72+statusBarHeight)
        }
        
        emailLabel.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(21)
            make.top.equalTo(nameLabel.snp.bottom).offset(49)
        }
        
        emailTextField.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(21)
            make.top.equalTo(emailLabel.snp.bottom).offset(10)
            make.height.equalTo(50)
        }
        
        pswLabel.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(21)
            make.top.equalTo(emailTextField.snp.bottom).offset(25)
        }
        
        pswTextField.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(21)
            make.top.equalTo(pswLabel.snp.bottom).offset(10)
            make.height.equalTo(50)
        }
        
        signInButton.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(21)
            make.height.equalTo(50)
            make.bottom.equalTo(signUpButton.snp.top).offset(-25)
        }
        
        signUpButton.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.bottom.equalTo(delegateLabel.snp.top).offset(-40)
        }
        
        delegateLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview().offset(-41-bottomSafeHeight)
        }
    }
    
    private lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.font = .bold(41)
        label.textColor = .white
        label.text = "Sign in"
        return label
    }()
    
    private lazy var emailLabel: UILabel = {
        let label = UILabel()
        label.font = .regular(14)
        label.textColor = .white
        label.text = "Email"
        return label
    }()
    
    private lazy var emailTextField: UITextField = {
        let tf = UITextField()
        tf.textType = .emailAddress
        tf.textColor = .white
        tf.font = .regular(14)
        tf.backgroundColor = .rgba(41, 32, 63)
        tf.layer.masksToBounds = true
        tf.layer.cornerRadius = 25
        tf.addPaddingLeft(25)
        return tf
    }()
    
    private lazy var pswLabel: UILabel = {
        let label = UILabel()
        label.font = .regular(14)
        label.textColor = .white
        label.text = "Passwords"
        return label
    }()
    
    private lazy var pswTextField: UITextField = {
        let tf = UITextField()
        tf.textType = .password
        tf.textColor = .white
        tf.font = .regular(14)
        tf.layer.masksToBounds = true
        tf.layer.cornerRadius = 25
        tf.addPaddingLeft(25)
        tf.backgroundColor = .rgba(41, 32, 63)
        return tf
    }()
    
    
    private lazy var signInButton: UIButton = {
        let btn = UIButton()
        let bgLayer1 = CAGradientLayer()
        bgLayer1.colors = [UIColor.rgba(218, 67, 255).cgColor, UIColor.rgba(82, 28, 255).cgColor]
        bgLayer1.locations = [0, 1]
        bgLayer1.frame = CGRect(x: 0, y: 0, width: SCREENWIDTH-38, height: 50)
        bgLayer1.startPoint = CGPoint(x: 1, y: 0.56)
        bgLayer1.endPoint = CGPoint(x: 0.56, y: 0.56)
        btn.layer.insertSublayer(bgLayer1, at: 0)
        
        btn.setTitle("Sign In", for: .normal)
        btn.titleLabel?.font = .medium(14)
        btn.setTitleColor(.white, for: .normal)
        
        btn.layer.masksToBounds = true
        btn.layer.cornerRadius = 25
        
        btn.addTarget(self, action: #selector(clickButton), for: .touchUpInside)
        return btn
    }()
    
    private lazy var signUpButton: UIButton = {
        let btn = UIButton()
        btn.setTitle("Sign Up", for: .normal)
        btn.setTitleColor(.rgba(255, 255, 255, 0.5), for: .normal)
        btn.titleLabel?.font = .medium(14)
        btn.addTarget(self, action: #selector(clickButton), for: .touchUpInside)
        return btn
    }()
    
    private lazy var delegateLabel: UILabel = {
        let label = UILabel()
        label.rz.colorfulConfer { confer in
            confer.text("Agree to the ")?.font(.regular(12)).textColor(.white)
            confer.text("Terms of Service")?.font(.regular(12)).textColor(.rgba(152, 48, 255)).tapActionByLable("term")
            confer.text(" and ")?.font(.regular(12)).textColor(.white)
            confer.text("Privacy Policy")?.font(.regular(12)).textColor(.rgba(255, 148, 31)).tapActionByLable("privacy")
        }
        
        label.rz.tapAction {[weak self] label, tapActionId, range in
            var url = ""
            if tapActionId == "term" {
                url = KTerms
            } else if tapActionId == "privacy" {
                url = KPolicy
            }
            
            let vc = COWebViewController(url)
            self?.navigationController?.pushViewController(vc)
        }
        return label
    }()
}

extension COSignInViewController {
    @objc
    private func clickButton(_ btn: UIButton) {
        if btn == signUpButton {
            let vc = COSignUpViewController()
            self.navigationController?.pushViewController(vc, animated: true)
        } else if btn == signInButton {
            self.signIn()
        }
    }
    
    private func signIn() {
        let email = emailTextField.text ?? ""
        let psw = pswTextField.text ?? ""
        guard !email.isEmpty else {
            self.view.makeToast("Please enter email", duration: 2, position: .center)
            return
        }
        guard !psw.isEmpty else {
            self.view.makeToast("Please enter password", duration: 2, position: .center)
            return
        }
        
        self.view.makeToastActivity(.center)
        CONetwork.sendRequest(provider: loginProvider, target: .signin(email: email, password: psw)) { text in
            guard let userInfo = COUserInfo.deserialize(from: text.dic) else {
                return
            }
            
            COUserManager.userInfo = userInfo
            self.updateMessage()
        } failedBlock: { _, text in
            self.view.hideToastActivity()
            self.view.makeToast(text, duration: 2, position: .center)
        }

    }
    
    private func updateMessage() {
        CONetwork.sendRequest(provider: loginProvider, target: .mine) { text in
            self.view.hideToastActivity()
            guard let userInfo = COUserInfo.deserialize(from: text) else {
                return
            }
            userInfo.token = COUserManager.userInfo?.token ?? ""
            COUserManager.userInfo = userInfo
            let vc = COHomeViewController()
            self.navigationController?.setViewControllers([vc], animated: true)
        } failedBlock: { _, text in
            self.view.hideToastActivity()
            self.view.makeToast(text, position: .center)
        }
    }
}
